package com.ibm.psbc.cloud;

import java.util.HashSet;
import java.util.Set;

import javax.annotation.PostConstruct;

import com.alibaba.csp.sentinel.adapter.gateway.common.SentinelGatewayConstants;
import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition;
import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPathPredicateItem;
import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPredicateItem;
import com.alibaba.csp.sentinel.adapter.gateway.common.api.GatewayApiDefinitionManager;
import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule;
import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayParamFlowItem;
import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayRuleManager;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;

import org.springframework.context.annotation.Configuration;

/**
 * @author Eric Zhao
 */
@Configuration
public class GatewayRuleConfig {

	@PostConstruct
	public void doInit() {
		// Prepare some gateway rules and API definitions (only for demo).
		// It's recommended to leverage dynamic data source or the Sentinel dashboard to
		// push the rules.
		initCustomizedApis();
		initGatewayRules();
	}

	private void initCustomizedApis() {
		Set<ApiDefinition> definitions = new HashSet<>();
		ApiDefinition api1 = new ApiDefinition("some_customized_api")
				.setPredicateItems(new HashSet<ApiPredicateItem>() {
					{
						add(new ApiPathPredicateItem().setPattern("/echo/**")
						// add(new ApiPathPredicateItem().setPattern("/aliyun_product/**")
						.setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX));
					}
				});
//        ApiDefinition api2 = new ApiDefinition("another_customized_api")
//            .setPredicateItems(new HashSet<ApiPredicateItem>() {{
//                add(new ApiPathPredicateItem().setPattern("/**")
//                    .setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX));
//            }});
		definitions.add(api1);
//        definitions.add(api2);
		GatewayApiDefinitionManager.loadApiDefinitions(definitions);
	}

	private void initGatewayRules() {
		Set<GatewayFlowRule> rules = new HashSet<>();

		/*
		 * rules.add(new
		 * GatewayFlowRule("aliyun-product-route").setCount(10).setIntervalSec(1));
		 * rules.add(new
		 * GatewayFlowRule("aliyun-product-route").setCount(2).setIntervalSec(2).
		 * setBurst(2).setParamItem( new
		 * GatewayParamFlowItem().setParseStrategy(SentinelGatewayConstants.
		 * PARAM_PARSE_STRATEGY_CLIENT_IP))); rules.add(new
		 * GatewayFlowRule("another-route-httpbin").setCount(10).setIntervalSec(1)
		 * .setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_RATE_LIMITER).
		 * setMaxQueueingTimeoutMs(600) .setParamItem(new GatewayParamFlowItem()
		 * .setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_HEADER)
		 * .setFieldName("X-Sentinel-Flag"))); rules.add(new
		 * GatewayFlowRule("another-route-httpbin").setCount(1).setIntervalSec(1)
		 * .setParamItem(new GatewayParamFlowItem()
		 * .setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_URL_PARAM).
		 * setFieldName("pa")));
		 */
		rules.add(new GatewayFlowRule("some_customized_api")
				.setResourceMode(SentinelGatewayConstants.RESOURCE_MODE_CUSTOM_API_NAME).setCount(2).setIntervalSec(1)
				.setParamItem(new GatewayParamFlowItem()
						.setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_URL_PARAM).setFieldName("pn"))
				);
		GatewayRuleManager.loadRules(rules);
	}
}